/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mop.dinnerbook.dao;

import com.mop.dinnerbook.bean.Floor;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Huge Laurie
 */
public class FloorDAO extends BaseDAO {
    public boolean addFloor(String fname){
        if(queryFloor(fname.trim()) != null){
            return false;
        }
        String isql = "insert into floors(fid,fname,valid) values(NULL,'" + fname + "',1)";
        boolean result = false;
        try {
                result = executeInsert(isql) > 0;
                return result;
        } catch (Exception e) {
                System.out.println(isql);
                e.printStackTrace();
        } 
        return false;
    }
    
    public Floor queryFloor(int fid){
        Floor floor = null;
        String sql="select fid,fname,valid from floors where fid = " + fid;
        try {
            floor = (Floor)executeSelectObject(sql, new Processor(){
                    @Override
                    public Object product(ResultSet rst) throws SQLException{
                            Floor floor = new Floor();
                            floor.setFid(rst.getInt(1));
                            floor.setFname(rst.getString(2));
                            floor.setValid(rst.getInt(3));
                            return floor;
                    }
            });
        } catch (SQLException e) {
                System.out.println(sql);
                e.printStackTrace();
        }
        return floor;
    }
    
    public Floor queryFloor(String fname){
        Floor floor = null;
        String sql="select fid,fname,valid from floors where valid = 1 and fname = '" + fname + "'";
        try {
            floor = (Floor)executeSelectObject(sql, new Processor(){
                    @Override
                    public Object product(ResultSet rst) throws SQLException{
                            Floor floor = new Floor();
                            floor.setFid(rst.getInt(1));
                            floor.setFname(rst.getString(2));
                            floor.setValid(rst.getInt(3));
                            return floor;
                    }
            });
        } catch (SQLException e) {
                System.out.println(sql);
                e.printStackTrace();
        }
        return floor;
    }
    
    public List getFloorList(){
        List result = null;
        String sql="select fid,fname,valid from floors where valid = 1" ;
        try {
            result = executeSelectList(sql, new Processor(){
                    @Override
                    public Object product(ResultSet rst) throws SQLException{
                            Floor floor = new Floor();
                            floor.setFid(rst.getInt(1));
                            floor.setFname(rst.getString(2));
                            floor.setValid(rst.getInt(3));
                            return floor;
                    }
            });
        } catch (SQLException e) {
                System.out.println(sql);
                e.printStackTrace();
        };
        return result;
    }
    
    public boolean changeFloor(int mid,int fid){
        if(queryFloor(fid) == null) return false;
        boolean result = false;
        String sql="update member set fid=" + fid + " where mid=" + mid;
        try {
		result = executeUpdate(sql) > 0;	
	} catch (SQLException e) {
                System.out.println(sql);
		e.printStackTrace();
	}
        return result;
    }
}
